package com.xuhj.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.xuhj.pojo.Dept;
import com.xuhj.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
 * @Author xuhj
 * @Description 提供Rest服务，一个微服务不管是不是web服务，都必须要写Controller，提供给别人调用的！！！！
 * @Date 10:44 下午 2021/7/28
 * @Param
 * @return
 **/
@RestController
public class DeptController {
   @Autowired
    private DeptService deptService;

   /**
    * 如果失败了，去调用该方法hystrixGet,这叫做服务熔断
   */
   @HystrixCommand(fallbackMethod = "hystrixGet")
   @GetMapping("/dept/get/{id}")
    public Dept get(@PathVariable("id") Long id){
       Dept dept = deptService.queryDeptById(id);
       if(dept == null){
           throw new RuntimeException("id不能为空");
       }
       return dept;
   }

    public Dept hystrixGet(@PathVariable("id") Long id){
        return new Dept().setDeptno(null).setDname("不存在该数据,请稍后查询");
    }

}
